
const {ccclass, property} = cc._decorator;

/**
 * Predefined variables
 * Name = test
 * DateTime = Thu Dec 23 2021 18:40:20 GMT+0800 (中国标准时间)
 * Author = linruimin
 * FileBasename = test.ts
 * FileBasenameNoExtension = test
 * URL = db://assets/test.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('test')
export default class test extends cc.Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;

    // arrName = ['kulou', 'cocosWalk', 'shitouWalk', 'altman', 'soldier', 'clown']
    arrName = ['kulou','shitouWalk', 'altman']

    arrModel: cc.Node[] = [];

    nodePool = new cc.NodePool('model');

    @property(cc.Node)
    modelParent = null;

    start () {
        this.arrName.forEach((name)=>{
            cc.loader.loadRes(`model/${name}`, cc.Prefab, (err, prefab) => {
                const newNode = cc.instantiate(prefab);
                this.arrModel.push(newNode);
            });
        });
    }

    onClickPut()
    {
        for (let i = 0; i < 100; i++)
        {
            this.randomPutNode();
        }
    }

    onClickPut1000()
    {
        for (let i = 0; i < 1000; i++)
        {
            this.randomPutNode();
        }
    }

    randomPutNode()
    {
        let randomNum = Math.floor(Math.random() * this.arrModel.length);
        let cloneNode = cc.instantiate(this.arrModel[randomNum]);
        this.nodePool.put(cloneNode);
    }

    onClickGet()
    {
        console.log(this.nodePool.size());
        while(this.nodePool.size() > 0)
        {
            console.log(this.nodePool.size());
            let node = this.nodePool.get();
            node.parent = this.modelParent;
            node.position = new cc.Vec3(Math.random()*500, Math.random()*300, Math.random()*50);
        }
    }

    // update (deltaTime: number) {
    //     // [4]
    // }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
